package com.dhu.service.impl;

import com.dhu.dao.UserMapper;
import com.dhu.pojo.User;
import com.dhu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    // 新增用户
    @Override
    public int addUser(User user) {
        try {
            return userMapper.addUser(user);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return -1;
        }
    }

    // 通过用户名查找用户
    @Override
    public User selectByUsername(String username) {
        try {
            return userMapper.selectByUsername(username);
        }catch (Exception e) {
            return null;
        }
    }

    // 通过用户名密码查询用户
    @Override
    public User selectByUsernameAndPassword(String username, String password) {
        try {
            return userMapper.selectByUsernameAndPassword(username,password);
        } catch (Exception e) {
            return null;
        }
    }

    // 通过token查询用户
    @Override
    public User selectByToken(String token) {
        try {
            return userMapper.selectByToken(token);
        } catch (Exception e) {
            return null;
        }
    }

    // 通过id查询用户
    @Override
    public User selectById(int id) {
        try {
            return userMapper.selectById(id);
        } catch (Exception e) {
            return null;
        }
    }

    // 更新用户信息
    @Override
    public int updateUserInfo(User user) {
        try {
            return userMapper.updateUserInfo(user);
        } catch (Exception e) {
            return -1;
        }
    }

    // 更新硬币
    @Override
    public int updateCoin(int num, int id ,String token) {
        try {
            return userMapper.updateCoin(num, id, token);
        } catch (Exception e) {
            return -1;
        }
    }

    // 更新用户头像
    @Override
    public int updateUserAvatar(String avatar, int id, String token) {
        try {
            return userMapper.updateUserAvatar(avatar, id, token);
        } catch (Exception e) {
            return -1;
        }
    }
}
